package com.td.framework.kqxt.jbpm.action;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.jbpm.api.ProcessDefinition;
import org.jbpm.api.ProcessInstance;
import org.jbpm.api.task.Task;

import com.td.framework.common.AbstractAction;
import com.td.framework.kqxt.domain.KqBqxx;
import com.td.framework.kqxt.domain.KqGlpbxx;
import com.td.framework.utils.JBPMUtil;
import com.td.framework.webmanager.page.PageService;

/**
 * 补签流程管理补签流程的增删改查信息
 * @author Administrator
 */
public class BqProcessAction extends AbstractAction {

    private List<ProcessDefinition> processList;//所有补签流程列表
    private List<KqBqxx> kqBqxxList;//补签信息列表
    private KqBqxx kqBqxx;//考勤补签信息实体
    private JBPMUtil jbpmUtil;
    Log log = LogFactory.getLog(BqProcessAction.class);

    /**
     * 补签流程列表
     * @return
     */
    public String listKqBqxx(){
    	String hql ="from KqBqxx t";    	
    	int rows = this.getBaseService().getRowsByListHql(hql);					
		page = PageService.getPage(rows, currentPage, pageSize, pageMethod);
		this.setCurrentPage(page.getCurentPage());		
		this.setPageSize(page.getPageSize());
		kqBqxxList = this.getBaseService().getPageList(hql,page.getStartRow(),pageSize);
		return "listKqBqxx";
    }
    
    /**
     * 打开补签流程添加页面
     */
    public String createKqBqxxForm(){
    	this.setEditState(1);
    	return "createKqBqxx";
    }
    
    /**
     * 添加补签流程信息
     * @return
     */
    public String createKqBqxx(){
	   boolean flag = true;
		try {
			kqBqxx.setCjr(getUserInfo().getYhm());
			kqBqxx.setCjsh(new Date());
			kqBqxx.setState(0);
	    	this.getBaseService().saveEntity(kqBqxx);
		} catch (Exception e) {
			 log.error("添加补签流程信息失败",e);
			 this.handleMessage("info", "添加失败,联系管理员!");
		}
		if(flag = true){
	    	 this.handleMessage("info", "添加成功，请继续操作!");
	     }		
	     return this.createKqBqxxForm();
    }
    
    /**
     * 提交补签流程
     */
    public String submitKqBqxx(){
    	Map map = new HashMap();
    	map.put("owner", getUserInfo().getYhm());
    	ProcessInstance processInstance = jbpmUtil.startPIById("eave-1", map);
    	KqBqxx bqxx =  (KqBqxx) this.getBaseService().getEntityById(KqBqxx.class, zids[0]);
    	bqxx.setState(1);
    	bqxx.setTjr(getUserInfo().getYhm());
    	bqxx.setTjsj(new Date());
    	this.getBaseService().update(KqBqxx.class);
    	//完成自己的填写补勤单的任务
    	List<Task> tasks =  jbpmUtil.findTasksByInstanseId(processInstance.getId());
    	if(tasks!=null && tasks.size()>0)
    		jbpmUtil.completeTask(tasks.get(0).getId());
    	return this.listKqBqxx();
    }
    
    /**
     * 删除选中补签流程的信息
     * @return
     */
    public String removeBqProcess(){
    	 boolean flag = false;  	     
  	     return this.listKqBqxx(); 
    }
    
    /**
     *打开补签流程设置展现页面
     * @return
     */
    public String showBqProcess(){
    	this.setEditState(2);    	
    	return "createKqBqxx";
    }
        
    
    /**
     * 生成流程图片
     * @return
     */
    public String createPic(){
    	/*ProcessEngine processEngine = Configuration.getProcessEngine();
    	RepositoryService repositoryService = processEngine
    			.getRepositoryService();
    	ExecutionService executionService = processEngine
    			.getExecutionService();
    	String id = request.getParameter("id");
    	ProcessInstance processInstance = executionService
    			.findProcessInstanceById(id);
    	String processDefinitionId = processInstance
    			.getProcessDefinitionId();
    	ProcessDefinition processDefinition = repositoryService
    			.createProcessDefinitionQuery().processDefinitionId(
    					processDefinitionId).uniqueResult();
    	InputStream inputStream = repositoryService.getResourceAsStream(processDefinition.getDeploymentId(),"loan.png");
    	byte[] b = new byte[1024];
    	int len = -1;
    	while ((len = inputStream.read(b, 0, 1024)) != -1) {
    		response.getOutputStream().write(b, 0, len);
    	}*/
    	return null;
    }

	public JBPMUtil getJbpmUtil() {
		return jbpmUtil;
	}

	public void setJbpmUtil(JBPMUtil jbpmUtil) {
		this.jbpmUtil = jbpmUtil;
	}

	public List<ProcessDefinition> getProcessList() {
		return processList;
	}

	public void setProcessList(List<ProcessDefinition> processList) {
		this.processList = processList;
	}

	public List<KqBqxx> getKqBqxxList() {
		return kqBqxxList;
	}

	public void setKqBqxxList(List<KqBqxx> kqBqxxList) {
		this.kqBqxxList = kqBqxxList;
	}

	public KqBqxx getKqBqxx() {
		return kqBqxx;
	}

	public void setKqBqxx(KqBqxx kqBqxx) {
		this.kqBqxx = kqBqxx;
	}    	
}
